const { Tray, Menu, nativeImage } = require('electron')
const path = require('path')

function createTray(mainWindow) {
  // 创建托盘图标
  const iconPath = path.join(__dirname, '../public/icon.png')
  const icon = nativeImage.createFromPath(iconPath)
    .resize({ width: 16, height: 16 })

  const tray = new Tray(icon)

  // 创建托盘菜单
  const contextMenu = Menu.buildFromTemplate([
    {
      label: '显示主窗口',
      click: () => {
        mainWindow.show()
      }
    },
    {
      label: '退出',
      click: () => {
        mainWindow.close()
        process.exit(0)
      }
    }
  ])

  // 设置托盘提示和菜单
  tray.setToolTip('消息提醒客户端')
  tray.setContextMenu(contextMenu)

  // 点击托盘图标显示/隐藏窗口
  tray.on('click', () => {
    if (mainWindow.isVisible()) {
      mainWindow.hide()
    } else {
      mainWindow.show()
    }
  })

  return tray
}

module.exports = { createTray }
